package com.calvin.lingting;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.*;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.*;
import com.calvin.lib.CActivity;
import com.calvin.lib.EventDispatch;
import com.calvin.lib.annotation.Notice;
import com.calvin.lib.annotation.ViewInject;
import com.calvin.lib.json.JSONObject;
import com.calvin.lib.notification.NotificationCenter;
import com.calvin.lingting.actions.MainAction;
import com.calvin.lingting.config.AppConfig;
import com.calvin.lingting.config.LrtsConfig;
import com.calvin.lingting.config.NTConfig;
import com.calvin.lingting.service.SerializationCenter;
import com.calvin.lingting.widget.NavigationFrameLayout;

import java.io.File;

public class MainActivity extends CActivity implements DialogInterface.OnClickListener {
    @ViewInject(id = R.id.main_actions)
    NavigationFrameLayout mFrameLayout;

    @ViewInject(isTag = true, tag = "btn_play")
    Button mButton;

    @ViewInject(id = R.id.foot_progress)
    ProgressBar mProgressBar;

    boolean mIsPlay = false;

    ImageView mFootView;
    Animation mAnimation;

    public FrameLayout getFrameLayout() {
        return mFrameLayout;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View rootView = View.inflate(this, R.layout.layout_main, null);
        setContentView(rootView);
        NotificationCenter.shared().scanNotice(this);
        NotificationCenter.shared().postNotice(NTConfig.NT_NAV_NEXT, R.layout.action_main, new MainAction());
        startService(new Intent(this, MediaPlayService.class));

        mFootView = (ImageView) findViewById(R.id.foot_image);
        Bitmap bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.zone_background)).getBitmap();
        mFootView.setImageBitmap(getRoundPhoto(bitmap));

        mAnimation = AnimationUtils.loadAnimation(this, R.anim.foot_imageview);

    }

    @Override
    protected void onStart() {
        super.onStart();
        SerializationCenter.shared().unSerializationData();
    }

    @Override
    protected void onPause() {
        SerializationCenter.shared().serializationData();
        super.onPause();
    }


    @ViewInject(isTag = true, tag = "btn_play")
    public void onPlayClick(View view) {
        NotificationCenter.shared().postNotice(mIsPlay ? NTConfig.NT_MD_PAUSE : NTConfig.NT_MD_RESUME);
    }

    @ViewInject(isTag = true, tag = "btn_next")
    public void onNextClick(View view) {
        NotificationCenter.shared().postNotice(NTConfig.NT_MD_NEXT);
    }


    @Notice(NTConfig.NT_CHANGE_MEDIA_INFO)
    public void onNtChangeFootInfo(Object... argv) {
        switch ((Integer) argv[0]) {
            case 0:
                mFootView = (ImageView) findViewById(R.id.foot_image);
                TextView footName = (TextView) findViewById(R.id.foot_name);
                TextView footDesc = (TextView) findViewById(R.id.foot_desc);
                JSONObject bookInfo = (JSONObject) argv[1];
                footName.setText(bookInfo.getStringValue(LrtsConfig.K_NAME));
                String tmp = MainActivity.sharedCurrent().getResources().getString(R.string.s_download_chapter);
                footDesc.setText(String.format(tmp + "-%s", argv[2], bookInfo.getStringValue(LrtsConfig.K_NAME)));
                String path = bookInfo.getStringValue(LrtsConfig.K_PHOTO);
                File file = Environment.getExternalStoragePublicDirectory(AppConfig.SD_PHOTO + "/" + path.split("/")[3]);
                if (file.exists()) {
                    mFootView.setImageBitmap(getRoundPhoto(BitmapFactory.decodeFile(file.getPath())));
                }
            case 1:
                mIsPlay = true;
                mFootView.setAnimation(mAnimation);
                mAnimation.start();
                mButton.setBackgroundResource(R.drawable.iconfont_zanting);
                break;
            case 2:
                mIsPlay = false;
                mAnimation.cancel();
                mButton.setBackgroundResource(R.drawable.iconfont_bofang);
                break;
            default:
                break;
        }

    }

    @Notice(NTConfig.NT_CHANGE_PROG)
    public void onProgressChange(Object... argv) {
        mProgressBar.setMax((Integer) argv[1]);
        mProgressBar.setProgress((Integer) argv[0]);
        SerializationCenter.shared().serializationBookList();
    }


    @Notice(NTConfig.NT_NAV_NEXT)
    public void onNavNextNotice(Object... argv) {
        if (argv.length == 2) {
            View.inflate(this, (Integer) argv[0], mFrameLayout);
            CActivity.initViewInject(argv[1], mFrameLayout.getChildAt(mFrameLayout.getChildCount() - 1));
        }
    }

    @Notice(NTConfig.NT_NAV_BACK)
    public void onNavNotice(Object... argv) {
        mFrameLayout.back();
    }

    private Bitmap getRoundPhoto(Bitmap bitmap) {
        int size = bitmap.getWidth() > bitmap.getHeight() ? bitmap.getHeight() : bitmap.getWidth();
        Bitmap output = Bitmap.createBitmap(size, size, Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        final Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.BLACK);
        canvas.drawCircle(size / 2, size / 2, size / 2, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, 0, 0, paint);
        return output;
    }

    @Override
    public void onBackPressed() {
        if (mFrameLayout.getChildCount() > 1) {
            mFrameLayout.back();
        } else {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle(R.string.s_app_name);
            builder.setMessage(R.string.s_exit);
            builder.setPositiveButton(R.string.s_confirm, this);
            builder.setNeutralButton(R.string.s_hide, this);
            builder.setNegativeButton(R.string.s_cancel, null);
            builder.create().show();
        }

    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
            case -1:
                System.exit(0);
            case -3:
                NotificationCenter.shared().clearNotice();
                this.finish();
                break;
        }
    }
}
